package com.hbean.controller;

import com.hbean.entity.SysAcc;
import com.hbean.entity.SysUser;
import com.hbean.service.SysUserService;
import com.hbean.system.config.SysCache;
import com.hbean.system.config.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 专注跳转一百年
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-09-28 23:46
 * @ModificationHistory:
 */

@Controller
public class RouterController {

    @Autowired
    private SysCache sysCache;
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = {"/","/toLogin"})
    public ModelAndView toLogin(ModelAndView model){
        model.addObject("sysName", SysConfig.serverName);
        model.addObject("codeFlag",sysCache.getSysDicByID("201810281502505324").getSd10());//验证码启用标志
        model.setViewName("login");
        return model;
    }

    @RequestMapping("/toMain")
    public ModelAndView toMain(ModelAndView model,HttpSession session){

        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(session.getAttribute("userId"))).collect(Collectors.toList());
        //获取个人头像
        List<SysAcc> sysAccs = sysCache.getSysAccByYwID(sysUsers.get(0).getSu00(),"201810301003289117");
        model.addObject("sysAcc",sysAccs.size() == 0 ? null : sysAccs.get(0));
        //获取用户名
        model.addObject("userName", sysCache.getSysUserByID((String) session.getAttribute("userId")).getSu01());
        //获取系统名称
        model.addObject("sysName", SysConfig.serverName);
        model.setViewName("index");
        return model;
    }

    @RequestMapping("/toSysUserList")
    public ModelAndView toSysUserList(ModelAndView model){
        model.setViewName("sys/user/sys_user_list");
        return model;
    }

    @RequestMapping("/toSysUserAdd")
    public ModelAndView toSysUserAdd(ModelAndView model){
        model.addObject("sex",sysCache.getSysDicByPiD("201810271045224137"));
        model.addObject("eduBg",sysCache.getSysDicByPiD("201810291644579183"));
        model.setViewName("sys/user/sys_user_list_add");
        return model;
    }

    @RequestMapping("/toSysUserMod")
    public ModelAndView toSysUserMod(ModelAndView model, HttpServletRequest request){

        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(request.getParameter("su00"))).collect(Collectors.toList());

        List<SysAcc> sysAccs = sysCache.getSysAccByYwID(sysUsers.get(0).getSu00(),"201810301003289117");

        model.addObject("sysUser",sysUsers.get(0));
        model.addObject("sysAcc",sysAccs.size() == 0 ? null : sysAccs.get(0));
        model.addObject("sex",sysCache.getSysDicByPiD("201810271045224137"));
        model.addObject("eduBg",sysCache.getSysDicByPiD("201810291644579183"));
        model.setViewName("sys/user/sys_user_list_mod");
        return model;
    }

    @RequestMapping("/toSysUserDetail")
    public ModelAndView toSysUserDetail(ModelAndView model, HttpServletRequest request){

        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(request.getParameter("su00"))).collect(Collectors.toList());

        List<SysAcc> sysAccs = sysCache.getSysAccByYwID(sysUsers.get(0).getSu00(),"201810301003289117");

        model.addObject("sysUser",sysUsers.get(0));
        model.addObject("sysAcc",sysAccs.size() == 0 ? null : sysAccs.get(0));
        model.addObject("sex",sysCache.getSysDicByPiD("201810271045224137"));
        model.addObject("eduBg",sysCache.getSysDicByPiD("201810291644579183"));
        model.setViewName("sys/user/sys_user_list_detail");
        return model;
    }

    @RequestMapping("/toSysUserRole")
    public ModelAndView toSysUserRole(ModelAndView model, HttpServletRequest request){

        model.addObject("su00",request.getParameter("su00"));
        model.addObject("userRoleS",sysCache.getSysUserRoleByUserID(request.getParameter("su00")));
        model.addObject("sysRoles",sysCache.getSysRoleByPiD("00001"));
        model.setViewName("sys/user/sys_user_list_role");
        return model;
    }

    @RequestMapping("/toSysDicList")
    public ModelAndView toSysDicList(ModelAndView model){
        model.setViewName("sys/dictionary/sys_dic_list");
        return model;
    }

    @RequestMapping("/toSysRoleList")
    public ModelAndView toSysRoleList(ModelAndView model){
        model.setViewName("sys/role/sys_role_list");
        return model;
    }

    @RequestMapping("/toSysMenuList")
    public ModelAndView toSysMenuList(ModelAndView model){
        model.setViewName("sys/menu/sys_menu_list");
        return model;
    }

    @RequestMapping("/toSysAuthList")
    public ModelAndView toSysAuthList(ModelAndView model){
        model.setViewName("sys/authority/sys_auth_list");
        return model;
    }

    @RequestMapping("/toSysLoginLogList")
    public ModelAndView toSysLoginLogList(ModelAndView model){
        model.setViewName("sys/log/sys_log_login_list");
        return model;
    }

    @RequestMapping("/toSysOptionLogList")
    public ModelAndView toSysOptionLogList(ModelAndView model){
        model.setViewName("sys/log/sys_log_option_list");
        return model;
    }

    @RequestMapping("/toLockPage")
    public ModelAndView toLockPage(ModelAndView model,HttpSession session){
        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(session.getAttribute("userId"))).collect(Collectors.toList());
        List<SysAcc> sysAccs = sysCache.getSysAccByYwID(sysUsers.get(0).getSu00(),"201810301003289117");
        model.addObject("sysAcc",sysAccs.size() == 0 ? null : sysAccs.get(0));
        model.setViewName("sys/index/index_lock");
        return model;
    }

    @RequestMapping("/toChoiceIcon")
    public ModelAndView toChoiceIcon(ModelAndView model){
        model.setViewName("sys/menu/sys_menu_list_icon");
        return model;
    }

    @RequestMapping("/toModPassword")
    public ModelAndView toModPassword(ModelAndView model){
        model.setViewName("sys/index/index_password");
        return model;
    }

    @RequestMapping("/toModBaseInfo")
    public ModelAndView toModBaseInfo(ModelAndView model,HttpServletRequest request){

        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> sysUser.getSu00().equals(request.getParameter("su00"))).collect(Collectors.toList());

        List<SysAcc> sysAccs = sysCache.getSysAccByYwID(sysUsers.get(0).getSu00(),"201810301003289117");

        model.addObject("sysUser",sysUsers.get(0));
        model.addObject("sysAcc",sysAccs.size() == 0 ? null : sysAccs.get(0));
        model.addObject("sex",sysCache.getSysDicByPiD("201810271045224137"));
        model.addObject("eduBg",sysCache.getSysDicByPiD("201810291644579183"));
        model.setViewName("sys/index/index_baseinfo");
        return model;
    }



}
